package com.service.Impl;

import com.dao.ArticleMapper;
import com.entity.pojo.Article;
import com.entity.vo.AjaxMassage;
import com.entity.vo.ArticlePage;
import com.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
public class ArticleServiceImpl implements ArticleService {

    @Autowired
    private ArticleMapper articleMapper;



    //添加图片
    public AjaxMassage addAjax(Article article, MultipartFile multipartFile, HttpServletRequest request, HttpSession session) {
        AjaxMassage ajaxMassage= new AjaxMassage();
        if (!multipartFile.isEmpty()) {
            String filename = multipartFile.getOriginalFilename();
            String suffix = filename.substring(filename.indexOf("."));
            if (multipartFile.getSize() < 10485760) {
                List<String> list = Arrays.asList(".jpg", ".jpeg", ".tif", ".png");
                if (list.contains(suffix)) {
                    String realPath = request.getServletContext().getRealPath("/upload/");
                    String paths = UUID.randomUUID().toString();
                    SimpleDateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd");
                    StringBuilder builder = new StringBuilder(dateFormat.format(new Date()));
                    builder.append("/");
                    String[] split = paths.split("-");
                    for (int i = 0; i < split.length-1; i++) {
                        builder.append(split[i]).append("/");
                    }
                    realPath+=builder.toString();

                    File file = new File(realPath);
                    if (!file.exists()) {
                        file.mkdirs();
                    }
                    String fileCurName= split[split.length-1]+suffix;
                    File file1 = new File(file, fileCurName);
                    try {
                        multipartFile.transferTo(file1);
                        article.setNewsImgUrl("/upload/"+builder.toString()+"/"+fileCurName);
                        article.setNewsId(UUID.randomUUID().toString());
                        article.setUserId((String) session.getAttribute("userId"));
                        article.setNewsPublishTime(new Date());
                        int b = articleMapper.insertSelective(article);
                        if (b>0){
                            ajaxMassage.setSuccess(true).setCode(200).setMassage("图片添加成功");
                        }else {
                            ajaxMassage.setSuccess(false).setCode(500).setMassage("数据执行失败");
                        }
                    } catch (IOException e) {
                        e.printStackTrace();
                        ajaxMassage.setSuccess(false).setCode(123).setMassage("图片添加失败");
                    }
                }else {
                    ajaxMassage.setSuccess(false).setCode(122).setMassage("图片格式不对");
                }

            }else {
                ajaxMassage.setSuccess(false).setCode(121).setMassage("图片太大");
            }

        }else {
            ajaxMassage.setSuccess(false).setCode(120).setMassage("图片为空");
        }


        return ajaxMassage;
    }

    @Override
    public int insertSelective(Article record) {
        return articleMapper.insertSelective(record);
    }

    @Override
    public boolean deleteByPrimaryKey(String newsId) {
        return articleMapper.deleteByPrimaryKey(newsId);
    }

    @Override
    public Article selecOneByTitle(String newsTitle) {
        return articleMapper.selecOneByTitle(newsTitle);
    }

    @Override
    public ArticlePage findByPage(Integer currentPage, Integer rows, String newsTitle, Byte newsState) {
        ArticlePage ArticlePage=new ArticlePage();
        //每页几条
        ArticlePage.setRows(rows);
        //总条数
        int count = articleMapper.findArticleTotalCount(newsTitle,newsState);
        ArticlePage.setTotalCount(count);

        int start=(currentPage-1)*rows;
        //每页的记录
        ArticlePage.setList(articleMapper.getAll(start,rows,newsState,newsTitle) );
        //总页数
        ArticlePage.setTotalPage((count%rows==0)?(count/rows):(count/rows+1));
        //当前页
        ArticlePage.setCurrentPage(currentPage);
        return ArticlePage;
    }

    @Override
    public Article selectByPrimaryKey(String newsId) {
        return articleMapper.selectByPrimaryKey(newsId);
    }

    @Override
    public boolean updateByPrimaryKeySelective(Article record) {
        return articleMapper.updateByPrimaryKeySelective(record);
    }
    //修改图片
    public AjaxMassage update(Article article, MultipartFile multipartFile, HttpServletRequest request, HttpSession session) {
        AjaxMassage ajaxMassage= new AjaxMassage();
        if (!multipartFile.isEmpty()) {
            String filename = multipartFile.getOriginalFilename();
            String suffix = filename.substring(filename.indexOf("."));
            if (multipartFile.getSize() < 10485760) {
                List<String> list = Arrays.asList(".jpg", ".jpeg", ".tif", ".png");
                if (list.contains(suffix)) {
                    String realPath = request.getServletContext().getRealPath("/upload/");
                    String paths = UUID.randomUUID().toString();
                    SimpleDateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd");
                    StringBuilder builder = new StringBuilder(dateFormat.format(new Date()));
                    builder.append("/");
                    String[] split = paths.split("-");
                    for (int i = 0; i < split.length-1; i++) {
                        builder.append(split[i]).append("/");
                    }
                    realPath+=builder.toString();

                    File file = new File(realPath);
                    if (!file.exists()) {
                        file.mkdirs();
                    }
                    String fileCurName= split[split.length-1]+suffix;
                    File file1 = new File(file, fileCurName);
                    try {
                        multipartFile.transferTo(file1);
                        article.setNewsImgUrl("/upload/"+builder.toString()+"/"+fileCurName);

                        boolean b = articleMapper.updateByPrimaryKey(article);
                        if (b==true){
                            ajaxMassage.setSuccess(true).setCode(200).setMassage("图片修改成功");
                        }else {
                            ajaxMassage.setSuccess(false).setCode(500).setMassage("数据执行失败");
                        }
                    } catch (IOException e) {
                        e.printStackTrace();
                        ajaxMassage.setSuccess(false).setCode(123).setMassage("图片修改失败");
                    }
                }else {
                    ajaxMassage.setSuccess(false).setCode(122).setMassage("图片格式不对");
                }

            }else {
                ajaxMassage.setSuccess(false).setCode(121).setMassage("图片太大");
            }

        }else {
            ajaxMassage.setSuccess(false).setCode(120).setMassage("图片为空");
        }


        return ajaxMassage;
    }

}
